package com.smartscreen.baseinfomanage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.smartscreen.baseinfomanage.entity.School;
import com.smartscreen.baseinfomanage.mapper.SchoolMapper;
import com.smartscreen.baseinfomanage.service.SchoolService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class SchoolServiceImpl implements SchoolService {

    @Autowired
    private SchoolMapper schoolMapper;

    @Override
    public List<School> getSchoolInfo() {
        // 获取所有学校信息记录
        return schoolMapper.selectList(null);
    }

    @Override
    public School updateSchoolInfo(School school) {
        // 获取现有学校信息（这里需要根据ID或其他条件来确定要更新的学校）
        if (school.getId() != null) {
            // 如果提供了ID，直接更新
            schoolMapper.updateById(school);
            return school;
        } else {
            // 如果没有提供ID，获取第一条记录进行更新
            QueryWrapper<School> queryWrapper = new QueryWrapper<>();
            queryWrapper.last("LIMIT 1");
            School existingSchool = schoolMapper.selectOne(queryWrapper);
            
            if (existingSchool != null) {
                // 更新字段
                if (school.getName() != null) {
                    existingSchool.setName(school.getName());
                }
                if (school.getLogoUrl() != null) {
                    existingSchool.setLogoUrl(school.getLogoUrl());
                }
                if (school.getIntroduction() != null) {
                    existingSchool.setIntroduction(school.getIntroduction());
                }
                schoolMapper.updateById(existingSchool);
                return existingSchool;
            } else {
                // 如果不存在，则创建新记录
                schoolMapper.insert(school);
                return school;
            }
        }
    }
}